package cn.xyy.system.api;

import cn.xyy.system.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.xyy.common.domain.system.auth.Department;
import cn.xyy.common.domain.system.auth.DepartmentVO;
import cn.xyy.common.annotation.log.Log;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.xyy.common.algorithm.DepartmentAlgorithm;
import cn.xyy.common.domain.mvc.R;

/**
 * 部门接口
 * 
 * @author xyy
 *
 */
@RestController
@RequestMapping(value = "/rest/department")
public class DepartmentRest {
	@Autowired
	private DepartmentService departmentService;

	/**
	 * 查询所有部门
	 *
	 * @return
	 */
	@RequestMapping(value = "getDeptList")
	public R getDeptList() {
		return R.ok(DepartmentAlgorithm.tree(departmentService.getAllDeptList()));
	}

	/**
	 * 保存部门
	 *
	 * @param dept
	 * @return
	 */
	@SaCheckPermission("dept:add")
	@Log("新增部门")
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public R add(@RequestBody Department dept) {
		departmentService.add(dept);
		return R.ok();
	}

	/**
	 * 修改
	 *
	 * @param dept
	 * @return
	 */
	@SaCheckPermission("dept:edit")
	@Log("修改部门")
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public R update(@RequestBody DepartmentVO vo) {
		departmentService.update(vo);
		return R.ok();
	}

	/**
	 * 移除
	 *
	 * @param id
	 * @return
	 */
	@SaCheckPermission("dept:del")
	@Log("删除部门")
	@RequestMapping(value = "delete")
	public R delete(@RequestParam("id") String id) {
		departmentService.delete(id);
		return R.ok();
	}
}
